//
//  StockInOutMenuView.swift
//  Yelowms
//
//  Created by Xwg on 2020/8/26.
//  Copyright © 2020 Yelopack. All rights reserved.
//

import Foundation
import UIKit

class StockInOutMenuView: UIView {
    
    var inAction: EmptyBlock?
    var outAction: EmptyBlock?
    
    /// 是否联动[外部是否动态需要 updateBgProgress(with:)]（ 默认：false）
    var isGanged = false
    
    @IBOutlet weak var bgView: UIView!
    @IBOutlet weak var inButton: UIButton!
    @IBOutlet weak var outButton: UIButton!
    
    @IBAction func inButtonEvent() {
        print("点击入库按钮")

        /* 如果已经点击了入库(当前按钮) -> 不重复点击 */
        guard outButton.isSelected else {return}
       
        /*不设置联动的时候走这个方法*/
        if !isGanged {
            inButton.isSelected = true
            outButton.isSelected = false
            UIView.animate(withDuration: 0.20) {
                self.bgView.transform = CGAffineTransform.identity
            }
        }
        
        inAction?()
    }
    
    
    @IBAction func outButtonEvent() {
        print("出库按钮点击")

        /* 如果已经点击了出库(当前按钮) -> 不重复点击 */
        guard inButton.isSelected else {return}
       
        /* 不联动的时候走这个方法*/
        if !isGanged {
            outButton.isSelected = true
            inButton.isSelected = false
            
            UIView.animate(withDuration: 0.20) {
                self.bgView.transform = CGAffineTransform(translationX: self.bgView.frame.width, y: 0)
            }
        }
        
        outAction?()
    }

    
    func updateBgViewProgress(with percent: CGFloat) {

        guard isGanged else { return }

        let percent = min(1, max(0, percent))
        let transformX: CGAffineTransform
        if percent == 0 {
            transformX = .identity
        } else {
            transformX = CGAffineTransform(translationX: bgView.frame.width * percent, y: 0)
        }
        bgView.transform = transformX
        inButton.isSelected = percent < 0.5
        outButton.isSelected = percent > 0.5
    }
    
}
